from django.shortcuts import render

# Create your views here.

import json
from . import usecases
from entities.err import *
from utils import ajax
from . helpers import jwt_required

def _check_result(result):
    err, data = result
    if err != ERROR_NO_ERROR:
        return ajax.error_response(err['message'], err['id'])

    return ajax.success_response(data)

def api_fetch_access_token(request):
    """ 获取AccessToken """
    return _check_result(usecases.api_fetch_access_token(request))

@jwt_required
def api_is_device_exists(request):
    """ 判断指定的设备是否存在 """
    data = json.loads(request.body)
    adid = data.get('adid', '')
    channel = data.get('channel', '')
    device_id = data.get('device_id', '')
    os_version = data.get('os_version', '')
    ip = data.get('ip', '')
    keys = data.get('keys', '')
    return _check_result(usecases.api_is_device_exists(request, adid, channel, device_id, os_version, ip, keys))

@jwt_required
def api_is_device_activated(request):
    """ 判断指定的设备是否被激活 """
    data = json.loads(request.body)
    adid = data.get('adid', '')
    channel = data.get('channel', '')
    device_id = data.get('device_id', '')
    os_version = data.get('os_version', '')
    ip = data.get('ip', '')
    keys = data.get('keys', '')
    return _check_result(usecases.api_is_device_activated(request, adid, channel, device_id, os_version, ip, keys))

if __name__ == '__main__':
    print('ok')
